package org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.mainFrameMenu;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.common.UserCommandListener;
import org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.table.FlightLogTableController;



public class MainFrameMenuController {
	
	private JMenuBar menuBar;
	
	private JMenu editMenu;
	
	private JMenuItem editMenuAddFlightItem;
	
	private JMenuItem editMenuEditFlightItem;
	
	private JMenuItem editMenuDeleteFlightItem;
	
	
	
	private static MainFrameMenuController INSTANCE;
	
	
	private MainFrameMenuController() {	
	}
	
	
	
	public static MainFrameMenuController instance(){
		
		if(INSTANCE == null){
			
			INSTANCE = new MainFrameMenuController();
		}
		return INSTANCE;
	}
	
	
	
	public JMenuBar getMenuBar(){
		
		if(menuBar == null){
			
			menuBar = new JMenuBar();
			
			
			menuBar.add(getEditMenu());
		}
		return menuBar;
	}
	
	
	
	private JMenu getEditMenu(){
		
		if(editMenu == null){
			
			editMenu = new JMenu("Edit");
			editMenu.add(getEditMenuAddFlightItem());
			editMenu.add(getEditMenuEditFlightItem());
			editMenu.add(getEditMenuDeleteFlightItem());
			
		}
		return editMenu;
	}

	
	
	private JMenuItem getEditMenuAddFlightItem(){
		
		if(editMenuAddFlightItem == null){
			
			editMenuAddFlightItem = new JMenuItem("Add Flight");
			
			editMenuAddFlightItem.addActionListener(UserCommandListener.instance());
			
			editMenuAddFlightItem.setActionCommand(UserCommandListener.EDIT_FLIGHT_RECORD);
		}
		return editMenuAddFlightItem;
		
	}

	
	private JMenuItem getEditMenuEditFlightItem(){
		
		if(editMenuEditFlightItem == null){
			
			editMenuEditFlightItem = new JMenuItem("Edit Flight");			
			
			editMenuEditFlightItem.addActionListener(UserCommandListener.instance());
			
			editMenuEditFlightItem.setActionCommand(UserCommandListener.EDIT_FLIGHT_RECORD);
		}
		return editMenuEditFlightItem;
	}


	
	private JMenuItem getEditMenuDeleteFlightItem(){
		
		if(editMenuDeleteFlightItem == null){
			
			editMenuDeleteFlightItem = new JMenuItem("Delete");			
			editMenuDeleteFlightItem.setActionCommand(UserCommandListener.DELETE_FLIGHT_RECORD);						
			editMenuDeleteFlightItem.addActionListener(UserCommandListener.instance());
		}
		return editMenuDeleteFlightItem;
	
	}
	

	/**
	 * Configures items based on whether or not there is a flight log rec 
	 * selected in the main display table
	 */
	public void configureItemsRequiringTableSelection() {

		boolean bTableHasSelection = FlightLogTableController.instance().isRowSelected();
		
		getEditMenuEditFlightItem().setEnabled(bTableHasSelection);
		getEditMenuEditFlightItem().setEnabled(bTableHasSelection);
		getEditMenuDeleteFlightItem().setEnabled(bTableHasSelection);
		
	}
	
}




